/*     */ package com.zimbra.cs.account.callback;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AttributeCallback;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class WhiteBlackList
/*     */   extends AttributeCallback
/*     */ {
/*     */   public void preModify(CallbackContext context, String attrName, Object attrValue, Map attrsToModify, Entry entry)
/*     */     throws ServiceException
/*     */   {
/*  38 */     if ((entry != null) && (!(entry instanceof Account))) {
/*  39 */       return;
/*     */     }
/*     */     
/*  42 */     Account acct = (Account)entry;
/*     */     String max;
/*     */     String max;
/*  45 */     if ((context.isCreate()) || (entry == null)) { String max;
/*  46 */       if ("amavisWhitelistSender".equalsIgnoreCase(attrName)) {
/*  47 */         max = context.getData(CallbackContext.DataKey.MAIL_WHITELIST_MAX_NUM_ENTRIES);
/*     */       } else
/*  49 */         max = context.getData(CallbackContext.DataKey.MAIL_BLACKLIST_MAX_NUM_ENTRIES);
/*     */     } else {
/*     */       String max;
/*  52 */       if ("amavisWhitelistSender".equalsIgnoreCase(attrName)) {
/*  53 */         max = acct.getAttr("zimbraMailWhitelistMaxNumEntries");
/*     */       } else {
/*  55 */         max = acct.getAttr("zimbraMailBlacklistMaxNumEntries");
/*     */       }
/*     */     }
/*     */     
/*  59 */     if (max != null) {
/*  60 */       check(max, acct, attrName, attrsToModify);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void postModify(CallbackContext context, String attrName, Entry entry) {}
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   private void check(String max, Account acct, String attrName, Map attrsToModify)
/*     */     throws ServiceException
/*     */   {
/*  76 */     int numMax = Integer.valueOf(max).intValue();
/*     */     
/*  78 */     Object replace = attrsToModify.get(attrName);
/*  79 */     Object add = attrsToModify.get("+" + attrName);
/*  80 */     Object remove = attrsToModify.get("-" + attrName);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  91 */     if (((add != null) || (remove != null)) && (replace != null)) {
/*  92 */       throw ServiceException.INVALID_REQUEST("can't mix +attrName/-attrName with attrName", null);
/*     */     }
/*     */     
/*     */ 
/*  96 */     if (replace != null)
/*     */     {
/*  98 */       Set<String> mods = getMultiValueSet(replace);
/*  99 */       if (mods.size() > numMax) {
/* 100 */         throwLimitExceeded(attrName, numMax);
/*     */       }
/*     */     }
/* 103 */     else if (acct == null)
/*     */     {
/*     */ 
/* 106 */       Set<String> mods = getMultiValueSet(add);
/* 107 */       if (mods.size() > numMax) {
/* 108 */         throwLimitExceeded(attrName, numMax);
/*     */       }
/*     */     } else {
/* 111 */       Set<String> curValues = acct.getMultiAttrSet(attrName);
/* 112 */       int curNum = curValues.size();
/*     */       
/*     */ 
/* 115 */       int numToAdd = 0;
/* 116 */       if (add != null) {
/* 117 */         Set<String> mods = getMultiValueSet(add);
/* 118 */         for (String s : mods) {
/* 119 */           if (!curValues.contains(s)) {
/* 120 */             numToAdd++;
/*     */           }
/*     */         }
/*     */       }
/*     */       
/* 125 */       int numToRemove = 0;
/* 126 */       if (remove != null) {
/* 127 */         Set<String> mods = getMultiValueSet(remove);
/* 128 */         for (String s : mods) {
/* 129 */           if (curValues.contains(s)) {
/* 130 */             numToRemove++;
/*     */           }
/*     */         }
/*     */       }
/*     */       
/*     */ 
/* 136 */       if (curNum > numMax)
/*     */       {
/* 138 */         if (add != null) {
/* 139 */           ZimbraLog.account.warn("number of values for " + attrName + " already exceeded the limit: " + numMax + ", additional values are ignored");
/*     */           
/*     */ 
/* 142 */           attrsToModify.remove("+" + attrName);
/*     */           
/*     */ 
/* 145 */           if (remove == null) {
/* 146 */             throwLimitExceeded(attrName, numMax);
/*     */           }
/*     */         }
/*     */       }
/*     */       else {
/* 151 */         int newNum = curNum + numToAdd - numToRemove;
/* 152 */         if (newNum > numMax) {
/* 153 */           throwLimitExceeded(attrName, numMax);
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void throwLimitExceeded(String attrName, int numMax) throws ServiceException
/*     */   {
/* 161 */     throw ServiceException.INVALID_REQUEST("exceed limit for " + attrName + ", max is " + numMax, null);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/callback/WhiteBlackList.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */